﻿using Hangfire;
using Volo.Abp.BackgroundWorkers.Hangfire;
using Volo.Abp.Uow;

namespace Demo.HangfireIntergation.Jobs
{
    public class MyJob1: HangfireBackgroundWorkerBase
    {
        public MyJob1()
        {
            RecurringJobId = nameof(MyJob1);
            CronExpression = Cron.Minutely();
        }

        /// <summary>
        /// 执行作业
        /// </summary>
        /// <param name="cancellationToken"></param>
        /// <returns></returns>
        public override Task DoWorkAsync(CancellationToken cancellationToken = default)
        {
            using (var uow = LazyServiceProvider.LazyGetRequiredService<IUnitOfWorkManager>().Begin())
            {
                Logger.LogInformation("执行 MyJob1..!");
                return Task.CompletedTask;
            }
        }

        /// <summary>
        /// 停止执行作业
        /// </summary>
        /// <param name="cancellationToken"></param>
        /// <returns></returns>
        public override Task StopAsync(CancellationToken cancellationToken = default)
        {
            Console.WriteLine("停止执行MyJob1.。。！");
            try
            {
                return base.StopAsync(cancellationToken);
            }
            catch (Exception e)
            {
                Console.WriteLine("任务状态错误");
                throw e;
            }
        }

    }
}
